from collections import defaultdict
def func(string):
     d = defaultdict(list)
     f = list(string)
     for i in f:
         for j in f:
             if j == i:
                 continue
             d[i].append(j)
     for i in f:
        d["0"].append(i)
     res = []
     index = "0"
     s = []
     def dfs(index):
         if len(s)==3:
             if "".join(s) not  in res:
                res.append("".join(s))
             return
         for j in d[index]:
             if j in s:
                 continue
             s.append(j)
             dfs(j)
             s.pop()
     dfs(index)
     print(res)
if __name__ == '__main__':
    func("abcd")
